<?php
namespace App\Services;
use App\Models\FeedBack;
use App\DTOS\Admin\FeedBackSearchDTO;
use App\DTOS\Admin\FeedBackInsertDTO;
use App\DTOS\Admin\FeedBackUpdateDTO;
// 游戏反馈管理
class FeedBackService
{
    /**
    * @var  \Illuminate\Database\Eloquent\Builder
    */
    private FeedBack $model;

    public function __construct()
    {
        $this->model = new FeedBack;

    }

    public function paginate(FeedBackSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $model = $this->model;
        $fdtype = $DTO->getFdtype();
        $roleid = $DTO->getRoleid();
        $packageNo = $DTO->getPackageNo();
        $content = $DTO->getContent();
        $serverid = $DTO->getServerid();
        $filenum = $DTO->getFilenum();

        /**反馈类型**/
        if (!is_null($fdtype)){
            $model = $model->where('fdtype ', $fdtype );
        }
        if (!is_null($filenum)){
            $model = $model->whereIn('filenum ', $filenum );
        }
        if (!is_null($roleid)){
            $model = $model->where('roleid ', $roleid );
        }
        if (!is_null($packageNo)){
            $model = $model->where('packageNo ', $packageNo );
        }
        if (!is_null($content)){
            $model = $model->whereIn('content ', $content );
        }

        if (!is_null($serverid)){
            $model = $model->where('serverid ', $serverid );
        }

        return $model->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    /**
    * @param  FeedBackInsertDTO $insertDTO
    * @return  int
    */
    public function insert(FeedBackInsertDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->model->insertGetId($data);
    }

    /**
    * @param  int $id
    * @param  FeedBackUpdateDTO $updateDTO
    */
    public function updateById($id,FeedBackUpdateDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->model->where('id',$id)->update($data);
    }

    public function deleteById($id)
    {
        $this->model->where('id',$id)->delete();
    }

    public function deleteByIds($ids)
    {
        if (empty($ids)) return;
        $this->model->whereIn('id',$ids)->delete();
    }
    public function getById($id)
    {
        return $this->model->where('id',$id)->first();
    }
}
